package my.diplom.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.apache.log4j.Logger;
import my.diplom.dao.IStatusDAO;
import my.diplom.entity.Status;

public class StatusDAOImpl implements IStatusDAO {


    private static final Logger log = Logger.getLogger(StatusDAOImpl.class);

    private HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    public void create(Status status) {
        log.debug("create status " + status.getStatusName());
        template.save(status);
    }

    public void delete(Status status) {
        log.debug("delete status = " + status.getStatusName());
        template.delete(status);

    }


    public List<Status> getAll() {
        log.debug("get all statuces");
        //noinspection JpaQlInspection
        return template.find("FROM Status order by statusName");
    }


    public Status getById(Long statusId) {
        log.debug("get status by id = " + statusId);
        return (Status) template.load(Status.class, statusId);
    }

    public Status getByName(String statusName) {
        try {
            log.debug("get status by status name = " + statusName);
            //noinspection JpaQlInspection
            return (Status) template.find("FROM Status s where s.statusName = ?", statusName).get(0);
        } catch (IndexOutOfBoundsException e) {
            log.error("Status does not exists", e);
            return null;
        }
    }


    public void update(Status status) {
        log.debug("update statud " + status.getStatusName());
        template.saveOrUpdate(status);

    }

    @Override
    public List<Status> getOrderStatuces() {
        return  template.find("from Status s where s.statusName in ('submitted', 'in_progress', 'approved', 'declined') ");
    }

    @Override
    public List<Status> getResourceStatuces() {
        return  template.find("from Status s where s.statusName in ('active', 'inactive') ");    
    }

    @Override
    public List<Status> getEventStatuces() {
        return  template.find("from Status s where s.statusName in ('completed', 'execute', 'not_started')");    
    }
}
